package com.sojoys.artifact.tools;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

/**
 * @UserName : SandKing
 * @DataTime : 2013年11月26日 下午10:29:35
 * @Description ：Please describe this document
 */
public class ToolDate {
	/**
	 * 毫秒
	 */
	public static final long TIME_MILLISECOND = 1;
	/**
	 * 秒
	 */
	public static final long TIME_SECOND = 1000 * TIME_MILLISECOND;
	/**
	 * 分钟
	 */
	public static final long TIME_MINUTE = 60 * TIME_SECOND;
	/**
	 * 小时
	 */
	public static final long TIME_HOUR = 60 * TIME_MINUTE;
	/**
	 * 天
	 */
	public static final long TIME_DAY = 24 * TIME_HOUR;
	/**
	 * 周
	 */
	public static final long TIME_WEEK = 7 * TIME_DAY;

	// 时间格式化
	public static final String fmt_yyyy_MM_dd_HH_mm_ss_sss = "yyyy-MM-dd HH:mm:ss.SSS";
	public static final String fmt_yyyy_MM_dd_HH_mm_ss = "yyyy-MM-dd HH:mm:ss";
	public static final String fmt_yyyy_MM_dd_HH_mm = "yyyy-MM-dd HH:mm";
	public static final String fmt_yyyy_MM_dd_HH = "yyyy-MM-dd HH";
	public static final String fmt_yyyy_MM_dd = "yyyy-MM-dd";
	public static final String fmt_HH_mm_ss = "HH:mm:ss";
	public static final String fmt_HH_mm = "HH:mm";
	public static final String fmt_MM_dd_HH_mm = "MM-dd HH:mm";
	public static final String fmt_MM_dd = "MM-dd";
	public static final String fmtMMddHHmmss = "MMddHHmmss";
	public static final String fmtYyyy = "yyyy";
	public static final String fmtYyyyMm = "yyyyMM";
	public static final String fmtYyyyMmDd = "yyyyMMdd";

	/** 时区偏移量 */
	public static long TIME_ZONE_OFFSET = TimeZone.getDefault().getOffset(new Date().getTime()) / 3600000;

	public static Date addWeek(int w, Date date) {
		return addMillisSecond(w * TIME_WEEK, date);
	}

	public static Date addDay(int d, Date date) {
		return addMillisSecond(d * TIME_DAY, date);
	}

	public static Date addHour(int h, Date date) {
		return addMillisSecond(h * TIME_HOUR, date);
	}

	public static Date addMinute(int m, Date date) {
		return addMillisSecond(m * TIME_MINUTE, date);
	}

	public static Date addSecond(int s, Date date) {
		return addMillisSecond(s * TIME_SECOND, date);
	}

	public static Date addMillisSecond(long t, Date date) {
		date.setTime(date.getTime() + t);
		return date;
	}

	/**
	 * 时间转字符串
	 * 
	 * @param format
	 * @param date
	 * @return
	 */
	public static String formatString(String format, Date date) {
		return new SimpleDateFormat(format).format(date);
	}

	/**
	 * 字符串转时间
	 * 
	 * @param dateStr
	 * @param formatStr
	 * @return
	 */
	public static Date formatDate(String dateStr, String formatStr) {
		DateFormat dd = new SimpleDateFormat(formatStr);
		Date date = null;
		try {
			date = dd.parse(dateStr);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}

	/**
	 * 判断是同一天
	 * 
	 * @param data1
	 * @param data2
	 * @return
	 */
	public static boolean isSameDay(Date data1, Date data2) {
		Calendar cal1 = Calendar.getInstance();
		Calendar cal2 = Calendar.getInstance();
		cal1.setTime(data1);
		cal2.setTime(data2);
		return (cal1.get(Calendar.ERA) == cal2.get(Calendar.ERA) && cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR)
				&& cal1.get(Calendar.DAY_OF_YEAR) == cal2.get(Calendar.DAY_OF_YEAR));
	}

	/**
	 * 星期天=1 星期6=7
	 * 
	 * @param date
	 * @return
	 */
	public static int getWeek(Date date) {
		Calendar cal1 = Calendar.getInstance();
		cal1.setTime(date);
		return cal1.get(Calendar.DAY_OF_WEEK);
	}

	/**
	 * 到指定时间当天凌晨0点的剩余毫秒数
	 * 
	 * @param currMillis
	 * @return
	 */
	public static long getTomorrowMillis(long currMillis) {
		return currMillis - ((currMillis + TIME_ZONE_OFFSET * TIME_HOUR) % TIME_DAY) + TIME_DAY;
	}
}
